In [1]:
%matplotlib inline
import LogReader as lr
import LogDataStructures as ls
fieldopt_path = "/home/einar/Documents/GitHub/PCG/FieldOpt/"
output_dir_path = "/home/einar/fo_out_20161118/"
hdf5_summary_file_name = "5SPOT.vars.h5"
reader = lr.LogReader(output_dir_path)
case_container = ls.CaseContainer(reader.case_log, reader.property_uuid_name_map)
optimizer = ls.Optimizer(reader.optimization_log, case_container)
simulator = ls.Simulator(reader.simulation_log)
production_data = ls.ProductionData(reader.production_data_log)
In [2]:
case_base = case_container.evaluated_cases[0]
case_tentative_best = optimizer.best_case_pr_iteration[-1]
case_latest = case_container.evaluated_cases[-1]
In [3]:
well_base_injector = case_base.get_well('INJECTOR')
well_base_producer = case_base.get_well('PRODUCER')
well_tentative_best_injector = case_tentative_best.get_well('INJECTOR')
well_tentative_best_producer = case_tentative_best.get_well('PRODUCER')
well_latest_injector = case_latest.get_well('INJECTOR')
well_latest_producer = case_latest.get_well('PRODUCER')
In [4]:
print('Current iteration:\t', optimizer.current_iteration)
print('Current step length:\t', optimizer.current_step_length)
print('Number of evaluated cases simulated in total:\t\t', optimizer.number_of_evaluated_cases)
print('Number of cases simulated this iteration:\t\t', optimizer.number_of_evaluated_cases_in_iteration)
print('Number of cases yet to be simulated this iteration:\t', optimizer.number_of_queued_cases_in_iteration)
In [5]:
from matplotlib import pyplot as plt
fig_ofv = plt.figure()
fig_ofv.set_size_inches(10,6)
ax_ofv = fig_ofv.add_subplot(111)
ax_ofv.plot([i+1 for i in range(len(optimizer.best_case_pr_iteration))],
[c.objective_function_value for c in optimizer.best_case_pr_iteration])
ax_ofv.set_xlabel('Iteration')
ax_ofv.set_ylabel('Objective function value')
ax_ofv.set_title('Objective function values per iteration')
Out[5]:
In [6]:
print('Iteration\tObjective function value')
for i in range(len(optimizer.best_case_pr_iteration)):
print (i+1, '\t\t', optimizer.best_case_pr_iteration[i].objective_function_value)
In [7]:
import matplotlib.gridspec as gridspec
grid_spec_wp = gridspec.GridSpec(12,12)
def setup_wp_figure(fig):
fig.set_size_inches(13,12)
ax_3d = fig.add_subplot(grid_spec_wp[0:8,:], projection='3d')
ax_xy = fig.add_subplot(grid_spec_wp[8:12,0:4])
ax_xz = fig.add_subplot(grid_spec_wp[8:12,4:8])
ax_yz = fig.add_subplot(grid_spec_wp[8:12,8:12])
fig.tight_layout(pad=2.5, w_pad=2.5, h_pad=2.5)
ax_3d.set_xlabel('x')
ax_3d.set_ylabel('y')
ax_3d.set_zlabel('z')
ax_xy.set_title('XY Projection')
ax_xy.set_xlabel('x')
ax_xy.set_ylabel('y')
ax_xz.set_title('XZ Projection')
ax_xz.set_xlabel('x')
ax_xz.set_ylabel('z')
ax_yz.set_title('YZ Projection')
ax_yz.set_xlabel('y')
ax_yz.set_ylabel('z')
ax_3d.set_xlim(0,1440)
ax_3d.set_ylim(0,1440)
ax_3d.set_zlim(1700,1724)
ax_xy.set_xlim(0,1440)
ax_xy.set_ylim(0,1440)
ax_xz.set_xlim(0,1440)
ax_xz.set_ylim(1700,1724)
ax_yz.set_xlim(0,1440)
ax_yz.set_ylim(1700,1724)
return (ax_3d, ax_xy, ax_xz, ax_yz)
def get_well_coords(well):
xs = [well.heel[0], well.toe[0]]
ys = [well.heel[1], well.toe[1]]
zs = [well.heel[2], well.toe[2]]
return (xs, ys, zs)
def plot_3d_well(ax, well, style):
(xs, ys, zs) = get_well_coords(well)
ax.plot(xs, ys, zs, style)
def plot_2d_well_xy(ax, well, style):
(xs, ys, zs) = get_well_coords(well)
ax.plot(xs, ys, style)
def plot_2d_well_xz(ax, well, style):
(xs, ys, zs) = get_well_coords(well)
ax.plot(xs, zs, style)
def plot_2d_well_yz(ax, well, style):
(xs, ys, zs) = get_well_coords(well)
ax.plot(ys, zs, style)
In [8]:
for well in [well_base_injector, well_base_producer]:
print(well.name)
print('\tHeel: ', well.heel)
print('\tToe: ', well.toe)
In [9]:
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig_well_paths_base = plt.figure()
(ax_base_wells_3d, ax_base_wells_xy, ax_base_wells_xz, ax_base_wells_yz) = setup_wp_figure(fig_well_paths_base)
plot_3d_well(ax_base_wells_3d, well_base_injector, 'b')
plot_3d_well(ax_base_wells_3d, well_base_producer, 'r--')
plot_2d_well_xy(ax_base_wells_xy, well_base_injector, 'b')
plot_2d_well_xy(ax_base_wells_xy, well_base_producer, 'r--')
plot_2d_well_xz(ax_base_wells_xz, well_base_injector, 'b')
plot_2d_well_xz(ax_base_wells_xz, well_base_producer, 'r--')
plot_2d_well_yz(ax_base_wells_yz, well_base_injector, 'b')
plot_2d_well_yz(ax_base_wells_yz, well_base_producer, 'r--')
In [10]:
for well in [well_tentative_best_injector, well_tentative_best_producer]:
print(well.name)
print('\tHeel: ', well.heel)
print('\tToe: ', well.toe)
In [11]:
from matplotlib import pyplot as plt
import matplotlib.gridspec as gridspec
from mpl_toolkits.mplot3d import Axes3D
fig_well_paths_best = plt.figure()
(ax_best_wells_3d, ax_best_wells_xy, ax_best_wells_xz, ax_best_wells_yz) = setup_wp_figure(fig_well_paths_best)
plot_3d_well(ax_best_wells_3d, well_tentative_best_injector, 'b')
plot_3d_well(ax_best_wells_3d, well_tentative_best_producer, 'r--')
plot_2d_well_xy(ax_best_wells_xy, well_tentative_best_injector, 'b')
plot_2d_well_xy(ax_best_wells_xy, well_tentative_best_producer, 'r--')
plot_2d_well_xz(ax_best_wells_xz, well_tentative_best_injector, 'b')
plot_2d_well_xz(ax_best_wells_xz, well_tentative_best_producer, 'r--')
plot_2d_well_yz(ax_best_wells_yz, well_tentative_best_injector, 'b')
plot_2d_well_yz(ax_best_wells_yz, well_tentative_best_producer, 'r--')
In [12]:
for well in [well_latest_injector, well_latest_producer]:
print(well.name)
print('\tHeel: ', well.heel)
print('\tToe: ', well.toe)
In [13]:
from matplotlib import pyplot as plt
import matplotlib.gridspec as gridspec
from mpl_toolkits.mplot3d import Axes3D
fig_well_paths_latest = plt.figure()
(ax_latest_wells_3d, ax_latest_wells_xy, ax_latest_wells_xz, ax_latest_wells_yz) = setup_wp_figure(fig_well_paths_latest)
plot_3d_well(ax_latest_wells_3d, well_latest_injector, 'b')
plot_3d_well(ax_latest_wells_3d, well_latest_producer, 'r--')
plot_2d_well_xy(ax_latest_wells_xy, well_latest_injector, 'b')
plot_2d_well_xy(ax_latest_wells_xy, well_latest_producer, 'r--')
plot_2d_well_xz(ax_latest_wells_xz, well_latest_injector, 'b')
plot_2d_well_xz(ax_latest_wells_xz, well_latest_producer, 'r--')
plot_2d_well_yz(ax_latest_wells_yz, well_latest_injector, 'b')
plot_2d_well_yz(ax_latest_wells_yz, well_latest_producer, 'r--')
In [14]:
from matplotlib import pyplot as plt
fig_sim_exec_time = plt.figure()
fig_sim_exec_time.set_size_inches(10,6)
ax_sim_exec_time = fig_sim_exec_time.add_subplot(121)
ax_sim_exec_time.plot([i+1 for i in range(len(simulator.durations))],
simulator.durations)
ax_sim_exec_time.set_xlabel('Simulation #')
ax_sim_exec_time.set_ylabel('Execution time [seconds]')
ax_sim_exec_time = fig_sim_exec_time.add_subplot(122)
ax_sim_exec_time.semilogy([i+1 for i in range(len(simulator.durations))],
simulator.durations)
ax_sim_exec_time.set_xlabel('Simulation #')
ax_sim_exec_time.set_ylabel('log(Execution time [seconds])')
Out[14]:
In [15]:
import numpy as np
simulator_durations = np.array(simulator.durations)
print('Min:\t', simulator_durations.min(), 's')
print('Max:\t', simulator_durations.max(), 's')
print('Median:\t', np.median(simulator_durations), 's')
print('Mean:\t', simulator_durations.mean(), 's')
In [18]:
from matplotlib import pyplot as plt
fig_result_history = plt.figure()
fig_result_history.set_size_inches(10,6)
ax_result_history = fig_result_history.add_subplot(111)
for case_id in production_data.time:
ax_result_history.plot(production_data.time[case_id], production_data.fopt[case_id], 'r')
ax_result_history.plot(production_data.time[case_id], production_data.fwpt[case_id], 'b')
ax_result_history.plot(production_data.time[case_id], production_data.fgpt[case_id], 'g')
In [19]:
import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as p3
import matplotlib.animation as animation
# Attaching 3D axis to the figure and setting props
fig_anim = plt.figure()
fig_anim.set_size_inches(14,12)
ax_anim = p3.Axes3D(fig_anim)
ax_anim.set_xlim(0,1440)
ax_anim.set_ylim(0,1440)
ax_anim.set_zlim(1700,1724)
ax_anim.set_xlabel('x')
ax_anim.set_ylabel('y')
ax_anim.set_zlabel('z')
def anim_get_well_lines(number):
wp = optimizer.best_case_pr_iteration[number].get_well("PRODUCER")
wi = optimizer.best_case_pr_iteration[number].get_well("INJECTOR")
return [[wp.heel, wp.toe], [wi.heel, wi.toe]]
def anim_update_well_lines(num, well_data, lines):
for i in range(len(lines)):
lines[i].set_data(well_data[num,i,:,0:2])
lines[i].set_3d_properties(well_data[num,i,:,2])
return lines
# The data to be plotted in a 4D array [case, inj/prod, heel/toe, x/y/z]
anim_well_data = np.array([anim_get_well_lines(cn) for cn in range(len(optimizer.best_case_pr_iteration))])
# Initializing lines. One line per well for the first best case.
anim_well_lines = [ax_anim.plot(well[:,0], well[:,1], well[:,2])[0] for well in anim_well_data[0]]
anim_well_lines[0].set_color('r')
anim_well_lines[1].set_color('b')
anim_well_lines[0].set_linestyle('--')
anim_well_lines[0].set_linewidth(4)
anim_well_lines[1].set_linewidth(4)
# FuncAnimation(figure, function, frames, fargs=(), interval,blit)
# figure: The figure in which to draw the animation
# function: function that will be called for each frame. The frame number will be passed as the first argument,
# followed by the arguments specified in fargs.
# frames: number of frames to draw; the number will be passed as first arg to the function ([0:frames])
# fargs will be passed as arguments to the function
# interval: ms between frames
line_ani = animation.FuncAnimation(fig_anim, anim_update_well_lines, len(optimizer.best_case_pr_iteration),
fargs=(anim_well_data, anim_well_lines),
interval=1000)
# Define a video tag for the browser
from tempfile import NamedTemporaryFile
VIDEO_TAG = '''<video controls autoplay>
<source src="data:{0}">
Your browser does not support the video tag.
</video>'''
# Function to convert the animation to a video format supported by the browser.
# Requires ffmpeg to be installed on the system.
def anim_to_html(anim):
if not hasattr(anim, '_encoded_video'):
with NamedTemporaryFile(suffix='.mp4') as f:
anim.save(f.name, fps=1, extra_args=['-vcodec', 'libx264'])
video = open(f.name, "rb").read()
anim.to_html5_video()
return VIDEO_TAG.format(anim.to_html5_video())
# Function that calls the converter function and returns HTML code to be displayed in the browser.
from IPython.display import HTML
def display_animation(anim):
plt.close(anim._fig)
return HTML(anim_to_html(anim))
# Display the video in the browser.
display_animation(line_ani)
Out[19]:
In [85]: